package com.pwt.controller.permission;

import com.pwt.annotation.Update;
import com.pwt.common.CustomException;
import com.pwt.common.DataNotFoundException;
import com.pwt.common.ResultPage;
import com.pwt.entity.MASCHINE;
import com.pwt.entity.WhatLine;
import com.pwt.entity.Role;
import com.pwt.model.ComboBoxModel;
import com.pwt.model.MenuTreeModel;
import com.pwt.model.RoleGrantsModel;
import com.pwt.service.WhatLineService;
import com.pwt.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "角色管理")
public class RoleController {
    @Autowired
    RoleService roleService;

    @Autowired
    WhatLineService whatLineService;

    @PostMapping("roles")
    @Operation(description = "新增角色",summary = "新增角色")
    public Role add(@RequestBody @Validated Role role) throws CustomException {
        return roleService.add(role);
    }

    @GetMapping("roles")
    @Operation(description = "查询角色",summary = "查询角色")
    public ResultPage<Role> list(@Parameter(description = "页码",required = true) @RequestParam Integer page,@Parameter(description = "容量",required = true) @RequestParam Integer limit,@RequestParam(required = false) String name) {
        return roleService.list(page,limit,name);
    }

    @GetMapping("roles/{id}")
    @Operation(description = "查询角色",summary = "查询角色")
    public Role role(@PathVariable @Parameter(description = "角色ID") String id) throws DataNotFoundException {
        return roleService.role(id);
    }

    @PutMapping("roles")
    @Operation(description = "修改角色",summary = "修改角色")
    public Role edit(@RequestBody @Validated(value = Update.class) Role role) throws CustomException {
        return roleService.edit(role);
    }

    @DeleteMapping("roles/{id}")
    @Operation(description = "删除角色",summary = "删除角色")
    public String delete(@PathVariable @Parameter(description = "角色ID") String id) throws CustomException {
        return roleService.delete(id);
    }

    @PostMapping("roles/grants")
    @Operation(description = "角色授权",summary = "角色授权")
    public void grants(@RequestBody RoleGrantsModel roleGrantsModel) {
        roleService.grants(roleGrantsModel);
    }

    @GetMapping("roles/{id}/menus")
    @Operation(description = "角色授权查询",summary = "角色授权查询")
    public MenuTreeModel menus(@PathVariable @Parameter(description = "角色ID") String id) {
        return roleService.menus(id);
    }

    @GetMapping("/maschines")
    @Operation(description = "选择机器",summary = "选择机器")
    public List<ComboBoxModel> getMaschine(@RequestParam(required = false) @Parameter(description = "机器名称或编号",required = false) String name) {
        return whatLineService.getMaschine(name);
    }

    @GetMapping("/menus-user")
    @Operation(description = "获取用户授权菜单",summary = "获取用户授权菜单")
    public MenuTreeModel getMenusByUser() {
        return roleService.getMenusByUser();
    }
}
